package com.zz.encryptiondemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {

    private String path="http://api.m.mtime.cn/Showtime/LocationMovies.api?locationId=%s";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
    }

    private void initData() {
        //URLEncoder编码
        String encode = null;
        try {
            encode = URLEncoder.encode("毛泽东","utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Log.d("flag", "----------------->initData: encode:" +encode);

        String decode = null;
        try {
            decode = URLDecoder.decode("%E6%AF%9B%E6%B3%BD%E4%B8%9C","utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Log.d("flag", "----------------->initData: decode:" +decode);

        new Thread(new Runnable() {
            @Override
            public void run() {
                InputStream is=null;
                ByteArrayOutputStream baso=null;
                try {
                    URL url=new URL(path);

                    HttpURLConnection conn= (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(4000);
                    if (conn.getResponseCode()==200){
                        is=conn.getInputStream();
                        int len=0;
                        byte[] b=new byte[1024];
                        while ((len=is.read(b))!=-1){
                            baso.write(b,0,len);
                        }
                        Log.d("flag", "----------------->run: " +new String(baso.toByteArray()));
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    Log.d("flag", "----------------->run: finally" );
                    if (baso != null) {
                        try {
                            baso.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        });

    }
}
